package com.sky.controller.user;

import com.sky.config.RedisConfiguration;
import com.sky.dto.DishDTO;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("DishUserController")
@Slf4j
@Api(tags = "前端菜品相关接口")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 通过ID获取菜品
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("通过分类ID获取菜品")
    public Result<List<DishVO>> queryDishByCateGoryId(Long categoryId){
        log.info("当前的分类ID：{}",categoryId);
        //首先设置当前的储存名称 例如 dish_? ?为当前菜品的分类
        String key = "dish_" + categoryId;
        //获取当前缓存中的菜品
        List<DishVO> dishVOList = (List<DishVO>)redisTemplate.opsForValue().get(key);
        //如果菜品存在则直接返回
        if (dishVOList != null && !dishVOList.isEmpty()){
            return Result.success(dishVOList);
        }
        //如果不存在则先设置后返回
        dishVOList = dishService.selectDishByCategoryIdGetVo(categoryId);
        redisTemplate.opsForValue().set(key,dishVOList);

        return Result.success(dishVOList);
    }

}
